
import lejos.nxt.LightSensor;
import lejos.nxt.SensorPort;


public class Tapefinder {
	
	public static final SensorPort LIGHT_PORT  = SensorPort.S4;
	private LightSensor ls;
	private int floorMax;
	private int floorMin;
	
	/**
	 * @param ls LightSensor 
	 * @param floorcolor color from lightsensor
	 * @param tolerance  tolerance of change in floorcolor
	 */
	public Tapefinder(LightSensor ls, int floorcolor, int tolerance){
		this.ls = ls;
		this.floorMax = floorcolor+tolerance;
		this.floorMin = floorcolor-tolerance;
	}
	
	public boolean tapeFound(){
		int light = ls.getLightValue();
		return (floorMax < light || floorMin > light);
		
	}
	/**
	 * Moves away from tape;
	 */
	public void avoidDanger(){
		/*
		 * Stops both engines, Checks if it's OK to go back, 
		 * 
		 */
		Moves.stop();
		Moves.backward(200);
		try{
			wait(1000);
		}catch (Exception e) {
			// TODO: handle exception
		}
		if(Eyes.checkAngle(90)) Moves.turnLeftAtSpot(90);
		else if(Eyes.checkAngle(-90)) Moves.turnRightAtSpot(90);
		else Moves.turnLeftAtSpot(Eyes.getFreeAngle());
		
		Alarm.roomUnsecure();
	}
	
}
